<?php

class ajax_authAjax
{
    private $common;
    public function  __construct()
    {
        $this -> common = new CommonQuery();
    }

    public function test()
    {
        echo json_encode($_POST['login']."  ".$_POST['password']);exit;
    }

    public function auth()
    {
        $message = array();
        $message['type'] = 'error';
        $sLogin = $_POST['login'];
        $sPass = md5($_POST['password']);

        if(isset($_SESSION[PREFIX]['user']['login']))
        {
                $message['content'] = "Jesteś już zalogowany";
        }
        else
        {
                if($sLogin!=""&&$sPass!="")
                {
                        $data['login'] = $sLogin;
                        $data['password'] = $sPass;
                        $aUser = $this -> common -> select('users', '*',$data);
                        if(isset($aUser[0]['login']))
                        {
                                if($aUser[0]['flag_confirm'])
                                {
                                        $_SESSION[PREFIX]['user']['login'] = $_POST['login'];
                                        $_SESSION[PREFIX]['user']['id'] = $aUser[0]['id'];
                                        $_SESSION[PREFIX]['user']['avatar'] = $aUser[0]['avatar'];
                                        $message['content'] = "Zostałeś zalogowany";
                                        $message['login'] = $_POST['login'];
                                        $message['avatar'] = $aUser[0]['avatar'];
                                        $message['type'] = "success";
                                }
                                else
                                {
                                        $message['content'] = "Twoje konto jest nie aktywne.";
                                }

                        }
                        else
                        {

                                $message['content'] = "Niepoprawny login lub hasło";

                        }
                }
                else
                {
                    $message['content'] = "Musisz podać login i hasło";

                }
        }

        echo json_encode($message);
    }
}
?>
